﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MapSurface.WidgetSurface;
using System.Xml;
using Microsoft.Surface.Presentation.Controls;
using Microsoft.Surface;
using Microsoft.Surface.Presentation;
using System.Windows;
using System.Windows.Controls;
using MapSurface.UIModelElement;

using System.IO;

namespace MapSurface.Engine
{
    public class UIModelLoader
    {
        #region Loader properties
        private String _docxmi = null;
        private string dllDir = null;

        public String DocXMI
        {
            get { return _docxmi; }
            set { _docxmi = value; }
        }
        private XamlObjects loader;
        private EditModelLoad edit = null;
        public XamlObjects XamlObject
        {
            get { return loader; }
            set { loader = value; }
        }

        public AssemblyReflexion.AssemblyReflexion Assembly
        {
            get {
                if (loader == null)
                    loader = new XamlObjects(_docxmi, "",dllDir);
                return loader.Assembly; 
            }
        }

        public XamlObjects Loader
        {
            get { return loader; }
            set { 
                loader = value; 
            }
        }

        #endregion
        #region Contructors

        public UIModelLoader()
        { 
        }
        public UIModelLoader(String  xmifile,string contextFile,string dllDir)
        {
            _docxmi = xmifile;
            loader = new XamlObjects(xmifile,contextFile,dllDir);
            edit = new EditModelLoad();
            this.dllDir = dllDir;
        }
        public void setLoader(string xmiFile,string dllDir)
        {
            _docxmi = xmiFile;
            loader = new XamlObjects(xmiFile, "",dllDir);
            this.dllDir = dllDir;
        }
        #endregion
        #region Model File Loader

        public static List<SurfaceListBoxItem> loadFile(string dirName)
        {
            List<SurfaceListBoxItem> ret = new List<SurfaceListBoxItem>();
            try
            {
                string[] filePaths = Directory.GetFiles(dirName);
                foreach (string file in filePaths)
                {
                    SurfaceListBoxItem item = new SurfaceListBoxItem();
                    
                    string [] items= file.Split('\\');
                    item.ContentStringFormat = file;
                    item.Content = items[items.Length - 1];
                    ret.Add(item);
                }
            }
            catch (Exception)
            {
            }
            return ret;
        }

        #endregion

        public Object process(Object context)
        {
            return null; //loader.load( context);
        }

        public void processEditMode(ref Object root,Object context)
        {
            if (edit == null)
                edit = new EditModelLoad();
            edit.visitEditMode(ref root, context);
        }

        public Object reprocess(String id)
        {
            //return loader.reLoad(id);
            return null;
        }

     }
        
}
